﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class XuatHang : System.Web.UI.Page
{
    SqlParameter[] p;
    clsXuLy ex = new clsXuLy();
    string frmName = "frmXuatHang";
    static int f_SuDung = 0, f_Them = 0, f_Sua = 0, f_Xoa = 0, f_XemTatCa = 0, f_InBaoCao = 0;
    public string Header = System.Configuration.ConfigurationSettings.AppSettings["Header"].ToString();
    static DataTable dt;
    static int PageNumber = 10;
    static int CurrentPage = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Convert.ToString(Session["user"])) || string.IsNullOrEmpty(Convert.ToString(Session["IDTramTron"])))
            GetMess("Hết phiên làm việc, vui lòng đăng nhập để tiếp tục", "Default.aspx");
        else
        {
            if (!IsPostBack)
            {
                string idnguoidung = Session["IDUser"].ToString();
                string sql = "select SuDung,Them,Sua,Xoa,InBaoCao,XemTatCa from XDAPhanQuyen where IDnguoidung='" + idnguoidung + "' and tag='" + frmName + "'";

                DataTable dt2 = ex.GetData_Text(sql);
                if (dt2.Rows.Count > 0)
                {
                    f_SuDung = int.Parse(dt2.Rows[0]["SuDung"].ToString());
                    f_Them = int.Parse(dt2.Rows[0]["Them"].ToString());
                    f_Sua = int.Parse(dt2.Rows[0]["Sua"].ToString());
                    f_Xoa = int.Parse(dt2.Rows[0]["Xoa"].ToString());
                    f_InBaoCao = int.Parse(dt2.Rows[0]["InBaoCao"].ToString());
                    f_XemTatCa = int.Parse(dt2.Rows[0]["XemTatCa"].ToString());
                }
                if (f_SuDung.Equals(1))
                {
                    Title = "Quản lý xuất hàng | " + Header;
                    

                    for (int i = 1; i <= 12; i++)
                    {
                        dlThang.Items.Add(new ListItem("Tháng " + i.ToString(), i.ToString()));
                    }
                    dlThang.Items.Insert(0, new ListItem("Theo thời gian", ""));
                    dlThang.SelectedIndex = 0;

                    int year1 = DateTime.Now.Year - 3;
                    int year2 = DateTime.Now.Year;
                    for (int i = year2; i >= year1; i--)
                    {
                        dlNam.Items.Add(new ListItem("Năm " + i.ToString(), i.ToString()));
                    }
                    dlNam.SelectedValue = year2.ToString();

                    dlLoaiKH.Items.Clear();
                    dlLoaiKH.DataSource = ex.GetData("sp_xdaloaikhachhang_List");
                    dlLoaiKH.DataBind();
                    dlLoaiKH.Items.Insert(0, new ListItem("--Chọn loại khách hàng--", ""));
                    dlLoaiKH.SelectedIndex = 0;

                    dlCongTrinhLoc.Items.Clear();
                    dlCongTrinhLoc.Items.Insert(0, new ListItem("--Chọn công trình--", ""));
                    dlCongTrinhLoc.SelectedIndex = 0;

                    txtTuNgay.Visible = true;
                    txtDenNgay.Visible = true;
                    dlNam.Visible = false;
                    txtTuNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
                    txtDenNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
                    dlThang.SelectedIndex = 0;

                    LoadLoaiSanPham(dlLoaiSanPham);
                    LoadPhuGia(dlLoaiPhuGia);
                    LoadCapPhoi(dlLoaiCapPhoi);
                    LoadTramTron(dlTramTron);
                    LoadCaLamViec(dlCaLamViec);
                    LoadMac(dlMacKH, "--Chọn mác khách hàng--");
                    LoadMac(dlMacTX, "--Chọn mác thực xuất--");

                    LoadNhaCungCap(dlNhaCungCap, "--Chọn nhà cung cấp--");
                    btnCancel_Click(sender, e);
                }
                else
                    GetMess("Bạn không có quyền xem trang này", "Home.aspx");
            }
        }
    }
    private void GetMess(string Mess, string Link)
    {
        if (Link == "")
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + Mess + "')", true);
        else
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + Mess + "');window.location.href='" + Link + "'", true);

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        double da12 = double.Parse(txtDa12.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDa12.Text.Trim().Replace(",", ""));
        double da24 = double.Parse(txtDa24.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDa24.Text.Trim().Replace(",", ""));
        
        p = new SqlParameter[1];
        p[0] = new SqlParameter("@ThoiGian", ex.GetNgayThang(txtNgayBan.Text.Trim()));
        p[0].SqlDbType = SqlDbType.SmallDateTime;
        string sophieu = ex.GetValueString("sp_GetSoPhieuXuat", p).Trim();

        if (ckMuaBeTong.Checked == false)
        {
            if (da12 <= 0 && da24 <= 0)
                gstGetMess("Nhập khối lượng đá", "");
            else
            {
                if (da12 > 0 && da24 > 0)
                    gstGetMess("Chỉ nhập khối lượng 1 loại đá", "");
                else
                {
                    btnSave.Enabled = false;
                    InsertUpdatePhieu(1, Guid.NewGuid(), sophieu);
                    UpdateAutoNumber(sophieu);
                    btnCancel_Click(sender, e);
                    gstGetMess("Lưu thành công", "");
                }
            }
        }
        else
        {
            if (dlNhaCungCap.SelectedIndex.Equals(0))
                gstGetMess("Chọn nhà cung cấp", "");
            else
            {
                btnSave.Enabled = false;
                InsertUpdatePhieu(1, Guid.NewGuid(),sophieu);
                UpdateAutoNumber(sophieu);
                btnCancel_Click(sender, e);
                gstGetMess("Lưu thành công", "");
            }
        }
    }

    private void InsertUpdatePhieu(int index, Guid IDs,string sophieu)
    {
        p = new SqlParameter[38];
        if (index == 1)
        {
            p[0] = new SqlParameter("@IDs", Guid.NewGuid());
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
        }
        else
        {
            p[0] = new SqlParameter("@IDs", IDs);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
        }

        
        p[1] = new SqlParameter("@SoPhieu", sophieu);
        p[1].SqlDbType = SqlDbType.VarChar;
        p[2] = new SqlParameter("@CaLamViec", ex.GetGuid(dlCaLamViec.SelectedValue));
        p[2].SqlDbType = SqlDbType.UniqueIdentifier;
        p[3] = new SqlParameter("@NgayXuat", ex.GetNgayThang(txtNgayBan.Text.Trim()));
        p[3].SqlDbType = SqlDbType.SmallDateTime;
        p[4] = new SqlParameter("@GioXuat", txtGioBan.Text.Trim());
        p[4].SqlDbType = SqlDbType.VarChar;
        p[5] = new SqlParameter("@TramTron", ex.GetGuid(dlTramTron.SelectedValue));
        p[5].SqlDbType = SqlDbType.UniqueIdentifier;
        p[6] = new SqlParameter("@LoaiCaBom", int.Parse(dlLoaiCaBom.SelectedValue));
        p[6].SqlDbType = SqlDbType.Int;
        p[7] = new SqlParameter("@BienSo", ex.GetGuid(dlBienSo.SelectedValue));
        p[7].SqlDbType = SqlDbType.UniqueIdentifier;
        p[8] = new SqlParameter("@LaiXe", ex.GetGuid(dlLaiXe.SelectedValue));
        p[8].SqlDbType = SqlDbType.UniqueIdentifier;
        p[9] = new SqlParameter("@LoaiSanPham", ex.GetGuid(dlLoaiSanPham.SelectedValue));
        p[9].SqlDbType = SqlDbType.UniqueIdentifier;
        p[10] = new SqlParameter("@LoaiPhuGia", ex.GetGuid(dlLoaiPhuGia.SelectedValue));
        p[10].SqlDbType = SqlDbType.UniqueIdentifier;
        p[11] = new SqlParameter("@LoaiCapPhoi", ex.GetGuid(dlLoaiCapPhoi.SelectedValue));
        p[11].SqlDbType = SqlDbType.UniqueIdentifier;
        p[12] = new SqlParameter("@TienHoTroCuoc", double.Parse(txtHoTroCuoc.Text.Trim().Replace(",", "").Equals("") ? "0" : txtHoTroCuoc.Text.Trim().Replace(",", "")));
        p[12].SqlDbType = SqlDbType.Float;
        p[13] = new SqlParameter("@BenMua", ex.GetGuid(dlBenMua.SelectedValue));
        p[13].SqlDbType = SqlDbType.UniqueIdentifier;
        p[14] = new SqlParameter("@CongTrinh", ex.GetGuid(dlCongTrinh.SelectedValue));
        p[14].SqlDbType = SqlDbType.UniqueIdentifier;
        p[15] = new SqlParameter("@HangMuc", ex.GetGuid(dlHangMuc.SelectedValue));
        p[15].SqlDbType = SqlDbType.UniqueIdentifier;

        p[16] = new SqlParameter("@KLTong", double.Parse(txtKLTong.Text.Trim().Replace(",", "").Equals("") ? "0" : txtKLTong.Text.Trim().Replace(",", "")));
        p[16].SqlDbType = SqlDbType.Float;
        p[17] = new SqlParameter("@KLBi", double.Parse(txtKLBi.Text.Trim().Replace(",", "").Equals("") ? "0" : txtKLBi.Text.Trim().Replace(",", "")));
        p[17].SqlDbType = SqlDbType.Float;
        p[18] = new SqlParameter("@KLHang", double.Parse(txtKLHang.Text.Trim().Replace(",", "").Equals("") ? "0" : txtKLHang.Text.Trim().Replace(",", "")));
        p[18].SqlDbType = SqlDbType.Float;

        p[19] = new SqlParameter("@MacTX", ex.GetGuid(dlMacTX.SelectedValue));
        p[19].SqlDbType = SqlDbType.UniqueIdentifier;
        p[20] = new SqlParameter("@MacKH", ex.GetGuid(dlMacKH.SelectedValue));
        p[20].SqlDbType = SqlDbType.UniqueIdentifier;

        p[21] = new SqlParameter("@DoSut", txtDoSut.Text.Trim());
        p[21].SqlDbType = SqlDbType.VarChar;
        p[22] = new SqlParameter("@KLThucXuat", double.Parse(txtKLTX.Text.Trim().Replace(",", "").Equals("") ? "0" : txtKLTX.Text.Trim().Replace(",", "")));
        p[22].SqlDbType = SqlDbType.Float;
        p[23] = new SqlParameter("@KLKhachHang", double.Parse(txtKLKhachHang.Text.Trim().Replace(",", "").Equals("") ? "0" : txtKLKhachHang.Text.Trim().Replace(",", "")));
        p[23].SqlDbType = SqlDbType.Float;
        p[24] = new SqlParameter("@KLNVKD", double.Parse(txtKLNVKD.Text.Trim().Replace(",", "").Equals("") ? "0" : txtKLNVKD.Text.Trim().Replace(",", "")));
        p[24].SqlDbType = SqlDbType.Float;
        p[25] = new SqlParameter("@Da12", double.Parse(txtDa12.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDa12.Text.Trim().Replace(",", "")));
        p[25].SqlDbType = SqlDbType.Float;
        p[26] = new SqlParameter("@Da24", double.Parse(txtDa24.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDa24.Text.Trim().Replace(",", "")));
        p[26].SqlDbType = SqlDbType.Float;
        p[27] = new SqlParameter("@CatVang", double.Parse(txtCatVang.Text.Trim().Replace(",", "").Equals("") ? "0" : txtCatVang.Text.Trim().Replace(",", "")));
        p[27].SqlDbType = SqlDbType.Float;
        p[28] = new SqlParameter("@XiMang", double.Parse(txtXiMang.Text.Trim().Replace(",", "").Equals("") ? "0" : txtXiMang.Text.Trim().Replace(",", "")));
        p[28].SqlDbType = SqlDbType.Float;
        p[29] = new SqlParameter("@Nuoc", double.Parse(txtNuoc.Text.Trim().Replace(",", "").Equals("") ? "0" : txtNuoc.Text.Trim().Replace(",", "")));
        p[29].SqlDbType = SqlDbType.Float;
        p[30] = new SqlParameter("@PhuGia", double.Parse(txtPhuGia.Text.Trim().Replace(",", "").Equals("") ? "0" : txtPhuGia.Text.Trim().Replace(",", "")));
        p[30].SqlDbType = SqlDbType.Float;
        p[31] = new SqlParameter("@KLTramTron", double.Parse(txtTongKLTram.Text.Trim().Replace(",", "").Equals("") ? "0" : txtTongKLTram.Text.Trim().Replace(",", "")));
        p[31].SqlDbType = SqlDbType.Float;
        p[32] = new SqlParameter("@GhiChu", txtGhiChu.Text.Trim());
        p[32].SqlDbType = SqlDbType.NVarChar;
        p[33] = new SqlParameter("@NgayTao", DateTime.Now);
        p[33].SqlDbType = SqlDbType.SmallDateTime;
        p[34] = new SqlParameter("@NguoiTao", Session["user"].ToString());
        p[34].SqlDbType = SqlDbType.NVarChar;
        p[35] = new SqlParameter("@MuaBeTong", ckMuaBeTong.Checked?1:0);
        p[35].SqlDbType = SqlDbType.Int;
        p[36] = new SqlParameter("@NhaCungCap", ex.GetGuid(dlNhaCungCap.SelectedValue));
        p[36].SqlDbType = SqlDbType.UniqueIdentifier;
        p[37] = new SqlParameter("@GiaMua", double.Parse(txtGiaMuaBeTong.Text.Trim().Replace(",", "").Equals("") ? "0" : txtGiaMuaBeTong.Text.Trim().Replace(",", "")));
        p[37].SqlDbType = SqlDbType.Float;
        if (index == 1)
            ex.ExecuteNonQuerry("sp_xdaxuathang_Add", p);
        else ex.ExecuteNonQuerry("sp_xdaxuathang_Update", p);
    }
    private void gstGetMess(string gstMess, string gstLink)
    {
        if (gstLink == "")
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "')", true);
        else
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "');window.location.href='" + gstLink + "'", true);

    }
    protected void btncapnhat_Click(object sender, EventArgs e)
    {
        double da12 = double.Parse(txtDa12.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDa12.Text.Trim().Replace(",", ""));
        double da24 = double.Parse(txtDa24.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDa24.Text.Trim().Replace(",", ""));
        if (ckMuaBeTong.Checked == false)
        {
            if (da12 <= 0 && da24 <= 0)
                gstGetMess("Nhập khối lượng đá", "");
            else
            {
                if (da12 > 0 && da24 > 0)
                    gstGetMess("Chỉ nhập khối lượng 1 loại đá", "");
                else
                {
                    Guid ids = new Guid(hdID.Value);
                    InsertUpdatePhieu(2, ids, "");
                    btnCancel_Click(sender, e);
                    GetMess("Cập nhật thành công", "");
                }
            }
        }
        else
        {
            if (dlNhaCungCap.SelectedIndex.Equals(0))
                gstGetMess("Chọn nhà cung cấp", "");
            else
            {
                Guid ids = new Guid(hdID.Value);
                InsertUpdatePhieu(2, ids,"");
                btnCancel_Click(sender, e);
                GetMess("Cập nhật thành công", "");
            }
        }

        
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        btnSave.Enabled = true;
        txtNgayBan.Text = DateTime.Now.ToString("dd/MM/yyyy");
        txtGioBan.Text = DateTime.Now.Hour.ToString() + "h" + DateTime.Now.Minute.ToString();

        

        lbTrangThai.Visible = false;
        hdID.Value = "";
        LoadSoPhieu();

        LoadBienSoXe(dlBienSo);

        LoadKhachHang(dlBenMua, "--Chọn khách hàng--");
        LoadCongTrinh(dlCongTrinh);
        LoadHangMuc(dlHangMuc);
        
        
        LoadLaiXeAll(dlLaiXe);

        LoadMacHD(dlMacHD);
        LoadLoaiDa(dlLoaiDa);
        dlTramTron.SelectedValue = Session["IDTramTron"].ToString();
        
        dlCaLamViec.SelectedIndex = 0;
        dlLoaiCaBom.SelectedIndex = 0;
        dlBienSo.SelectedIndex = 0;
        dlLaiXe.SelectedIndex = 0;
        dlLoaiSanPham.SelectedIndex = 0;
        dlLoaiPhuGia.SelectedIndex = 0;
        dlLoaiCapPhoi.SelectedIndex = 0;

        dlNhaCungCap.SelectedIndex = 0;
        ckMuaBeTong.Checked = false;


        txtHoTroCuoc.Text = "";
        dlBenMua.SelectedIndex = 0;
        dlCongTrinh.SelectedIndex = 0;
        dlHangMuc.SelectedIndex = 0;
        txtKLTong.Text = "";
        txtKLBi.Text = "";
        txtKLHang.Text = "";
        dlMacKH.SelectedIndex = 0;
        dlMacTX.SelectedIndex = 0;
        txtDoSut.Text = "";
        txtKLTX.Text = "";
        txtKLKhachHang.Text = "";
        txtKLNVKD.Text = "";
        txtDa12.Text = "";
        txtDa24.Text = "";
        txtCatVang.Text = "";
        txtXiMang.Text = "";
        txtNuoc.Text = "";
        txtPhuGia.Text = "";
        txtTongKLTram.Text = "";
        txtGhiChu.Text = "";
        txtNVKD.Text = "";
        txtGiaMuaBeTong.Text = "";

        txtPhuGia.ReadOnly = true;

        dlCaLamViec.Enabled = true;
        dlTramTron.Enabled = true;
        dlBienSo.Enabled = true;
        dlLaiXe.Enabled = true;
        dlLoaiSanPham.Enabled = true;
        dlLoaiPhuGia.Enabled = true;
        dlLoaiCapPhoi.Enabled = true;
        dlBenMua.Enabled = true;
        dlCongTrinh.Enabled = true;
        dlHangMuc.Enabled = true;
        dlMacKH.Enabled = true;
        dlMacTX.Enabled = true;

        dlNhaCungCap.Enabled = false;
        txtGiaMuaBeTong.Enabled = false;

        txtNgayBan.Enabled = true;
        hdID.Value = "";
        btnSave.Visible = CheckKhoa2(txtNgayBan.Text.Trim(), frmName, Session["user"].ToString());
        btncapnhat.Visible = false;

        btnNext.Visible = true;
        btnPre.Visible = true;
        btnFirst.Visible = true;

        CurrentPage = 1;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdaxuathang_List", p);
        GV.DataBind();

    }
    private bool CheckKhoa2(string ngaythang, string name, string nhanvien)
    {
        //load trang thai khoa
        if (!ngaythang.Trim().Equals(""))
        {

            p = new SqlParameter[3];
            p[0] = new SqlParameter("@Name", name);
            p[0].SqlDbType = SqlDbType.NVarChar;
            p[1] = new SqlParameter("@Ngay", ex.GetNgayThang(ngaythang));
            p[1].SqlDbType = SqlDbType.SmallDateTime;
            p[2] = new SqlParameter("@NhanVien", nhanvien);
            p[2].SqlDbType = SqlDbType.NVarChar;

            string index = ex.GetValueString("sp_KhoaMo_Check", p);

            if (index.Trim().Equals("2"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else return false;
    }
    void UpdateAutoNumber(string sophieu)
    {
        //p = new SqlParameter[1];
        //p[0] = new SqlParameter("@ThoiGian", ex.GetNgayThang(txtNgayBan.Text.Trim()));
        //p[0].SqlDbType = SqlDbType.SmallDateTime;
        //string sophieu = ex.GetValueString("sp_GetSoPhieuXuat", p).Trim();

        p = new SqlParameter[4];
        p[0] = new SqlParameter("@Name", "XuatHang");
        p[0].SqlDbType = SqlDbType.VarChar;
        p[1] = new SqlParameter("@Number", int.Parse(sophieu));
        p[1].SqlDbType = SqlDbType.Int;
        p[2] = new SqlParameter("@Ngay", ex.GetNgayThang(txtNgayBan.Text.Trim()));
        p[2].SqlDbType = SqlDbType.SmallDateTime;
        p[3] = new SqlParameter("@LoaiVL", DBNull.Value);
        p[3].SqlDbType = SqlDbType.UniqueIdentifier;
        ex.ExecuteNonQuerry("sp_vnttblautonumber_Add", p);

    }
    void LoadSoPhieu()
    {
        try
        {
            p = new SqlParameter[1];
            p[0] = new SqlParameter("@ThoiGian", ex.GetNgayThang(txtNgayBan.Text.Trim()));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            string sophieu = ex.GetValueString("sp_GetSoPhieuXuat", p).Trim();
            txtSoPhieu.Text = sophieu;
        }
        catch
        {
            txtSoPhieu.Text = "";
        }
    }
    void LoadMac(DropDownList dl,string title)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_xdamacbetong_ListDrop");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem(title, ""));
        dl.SelectedIndex = 0;
    }
    void LoadMacAll(DropDownList dl, string title)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_xdamacbetong_ListAll");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem(title, ""));
        dl.SelectedIndex = 0;
    }
    void LoadCaLamViec(DropDownList dl)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_xdacalamviec_ListDrop");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem("--Chọn ca làm việc--", ""));
        dl.SelectedIndex = 0;
    }
    void LoadCaLamViecAll(DropDownList dl)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_xdacalamviec_ListAll");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem("--Chọn ca làm việc--", ""));
        dl.SelectedIndex = 0;
    }
    void LoadLoaiSanPham(DropDownList dl)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_xdaloaisanpham_ListDrop");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem("--Chọn loại sản phẩm--", ""));
        dl.SelectedIndex = 0;
    }
    void LoadLoaiSanPhamAll(DropDownList dl)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_xdaloaisanpham_ListAll");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem("--Chọn loại sản phẩm--", ""));
        dl.SelectedIndex = 0;
    }
    void LoadPhuGia(DropDownList dl)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_xdaxuathang_ListLoaiPhuGia");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem("--Chọn loại phụ gia--", ""));
        dl.SelectedIndex = 0;
    }
    void LoadPhuGiaAll(DropDownList dl)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_xdaloaiphugia_ListAll");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem("--Chọn loại phụ gia--", ""));
        dl.SelectedIndex = 0;
    }
    void LoadCapPhoi(DropDownList dl)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_xdaloaicapphoi_ListDrop");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem("--Chọn loại cấp phối--", ""));
        dl.SelectedIndex = 0;
    }
    void LoadCapPhoiAll(DropDownList dl)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_xdaloaicapphoi_ListAll");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem("--Chọn loại cấp phối--", ""));
        dl.SelectedIndex = 0;
    }
    
    void LoadCongTrinh(DropDownList dl)
    {
        dl.Items.Clear();
        dl.Items.Insert(0, new ListItem("--Chọn công trình--", ""));
        dl.SelectedIndex = 0;
    }
    void LoadCongTrinhAll(DropDownList dl)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_xdacongtrinh_ListAll");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem("--Chọn công trình--", ""));
        dl.SelectedIndex = 0;
    }
    void LoadHangMuc(DropDownList dl)
    {
        dl.Items.Clear();
        dl.Items.Insert(0, new ListItem("--Chọn hạng mục--", ""));
        dl.SelectedIndex = 0;
    }
    void LoadHangMucAll(DropDownList dl)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_xdahangmuc_ListAll");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem("--Chọn hạng mục--", ""));
        dl.SelectedIndex = 0;
    }
    
    void LoadKhachHang(DropDownList dl, string title)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_xdakhachhang_ListDrop");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem(title, ""));
        dl.SelectedIndex = 0;
    }
    void LoadKhachHangAll(DropDownList dl, string title)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_xdakhachhang_ListAll");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem(title, ""));
        dl.SelectedIndex = 0;
    }
    void LoadBienSoXe(DropDownList dlBienSo)
    {
        p = new SqlParameter[1];
        p[0] = new SqlParameter("@ThoiGian", ex.GetNgayThang(txtNgayBan.Text.Trim()));
        p[0].SqlDbType = SqlDbType.SmallDateTime;

        dlBienSo.Items.Clear();
        dlBienSo.DataSource = ex.GetData("sp_xdaxuathang_GetBienSo", p);
        dlBienSo.DataBind();
        dlBienSo.Items.Insert(0, new ListItem("--Chọn biển số xe--", ""));
        dlBienSo.SelectedIndex = 0;
    }
    void LoadBienSoXeAll(DropDownList dlBienSo)
    {
        
        dlBienSo.Items.Clear();
        dlBienSo.DataSource = ex.GetData("sp_xdadoituongchi_ListAll");
        dlBienSo.DataBind();
        dlBienSo.Items.Insert(0, new ListItem("--Chọn biển số xe--", ""));
        dlBienSo.SelectedIndex = 0;
    }
    void LoadTramTron(DropDownList dl)
    {
        dlTramTron.Items.Clear();
        dlTramTron.DataSource = ex.GetData("sp_xdaxuathang_ListTramTron");
        dlTramTron.DataBind();
        dlTramTron.Items.Insert(0, new ListItem("--Chọn trạm trộn--", ""));
        dlTramTron.SelectedIndex = 0;
    }
    void LoadMacHD(DropDownList dl)
    {
        dl.Items.Clear();
        dl.Items.Insert(0, new ListItem("--Chọn mác HĐ--", ""));
        dl.SelectedIndex = 0;
    }
    void LoadLoaiDa(DropDownList dl)
    {
        dl.Items.Clear();
        dl.Items.Insert(0, new ListItem("--Chọn loại đá--", "0"));
        dl.SelectedIndex = 0;
    }
    void LoadTramTronAll(DropDownList dl)
    {
        dlTramTron.Items.Clear();
        dlTramTron.DataSource = ex.GetData("sp_xdatbltramtron_ListAll");
        dlTramTron.DataBind();
        dlTramTron.Items.Insert(0, new ListItem("--Chọn trạm trộn--", ""));
        dlTramTron.SelectedIndex = 0;
    }
    
    void LoadLaiXeAll(DropDownList dlLaiXe)
    {
        dlLaiXe.Items.Clear();
        dlLaiXe.DataSource = ex.GetData("sp_xdaxuathang_ListLaiXeAll");
        dlLaiXe.DataBind();
        dlLaiXe.Items.Insert(0, new ListItem("--Chọn lái xe--", ""));
        dlLaiXe.SelectedIndex = 0;
    }
    
    protected void dlBenMua_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dlBenMua.SelectedIndex > 0)
        {
            p = new SqlParameter[1];
            p[0] = new SqlParameter("@KhachHang", ex.GetGuid(dlBenMua.SelectedValue));
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            dlCongTrinh.Items.Clear();
            dlCongTrinh.DataSource = ex.GetData("sp_xdaxuathang_LoadCongTrinhByKhachHang", p);
            dlCongTrinh.DataBind();
            dlCongTrinh.Items.Insert(0, new ListItem("--Chọn công trình--", ""));
            dlCongTrinh.SelectedIndex = 0;
        }
        else
        {
            dlCongTrinh.Items.Clear();
            dlCongTrinh.Items.Insert(0, new ListItem("--Chọn công trình--", ""));
            dlCongTrinh.SelectedIndex = 0;
        }
    }
    protected void dlCongTrinh_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dlCongTrinh.SelectedIndex > 0)
        {
            p = new SqlParameter[1];
            p[0] = new SqlParameter("@CongTrinh", ex.GetGuid(dlCongTrinh.SelectedValue));
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            dlHangMuc.Items.Clear();
            dlHangMuc.DataSource = ex.GetData("sp_xdaxuathang_LoadHangMucByCongTrinh", p);
            dlHangMuc.DataBind();
            dlHangMuc.Items.Insert(0, new ListItem("--Chọn công trình--", ""));
            dlHangMuc.SelectedIndex = 0;
        }
        else
        {
            dlHangMuc.Items.Clear();
            dlHangMuc.Items.Insert(0, new ListItem("--Chọn công trình--", ""));
            dlHangMuc.SelectedIndex = 0;
        }
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@NgayBan", ex.GetNgayThang(txtNgayBan.Text.Trim()));
        p[0].SqlDbType = SqlDbType.SmallDateTime;
        p[1] = new SqlParameter("@CongTrinh", ex.GetGuid(dlCongTrinh.SelectedValue));
        p[1].SqlDbType = SqlDbType.UniqueIdentifier;

        txtNVKD.Text = ex.GetValueString("sp_xdaxuathang_GetNVKDByCongTrinh", p);
        //
    }
    protected void dlBienSo_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dlBienSo.SelectedIndex > 0)
        {
            p = new SqlParameter[2];
            p[0] = new SqlParameter("@NgayBan", ex.GetNgayThang(txtNgayBan.Text.Trim()));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@BienSo", ex.GetGuid(dlBienSo.SelectedValue));
            p[1].SqlDbType = SqlDbType.UniqueIdentifier;
            string laixe = ex.GetValueString("sp_xdaxuathang_GetLaiXeByBienSo", p);
            dlLaiXe.SelectedValue = laixe;
        }
        else
        {
            dlLaiXe.Items.Clear();
            dlLaiXe.Items.Insert(0, new ListItem("--Chọn lái xe--", ""));
            dlLaiXe.SelectedIndex = 0;
        }
    }
    protected void txtKLTong_TextChanged(object sender, EventArgs e)
    {
        double tltong = double.Parse(txtKLTong.Text.Trim().Replace(",", "").Equals("") ? "0" : txtKLTong.Text.Trim().Replace(",", ""));
        double tlbi = double.Parse(txtKLBi.Text.Trim().Replace(",", "").Equals("") ? "0" : txtKLBi.Text.Trim().Replace(",", ""));
        txtKLHang.Text = ex.ConvertDecimal(tltong - tlbi);

        txtKLBi.Focus();
    }
    protected void txtKLBi_TextChanged(object sender, EventArgs e)
    {
        double tltong = double.Parse(txtKLTong.Text.Trim().Replace(",", "").Equals("") ? "0" : txtKLTong.Text.Trim().Replace(",", ""));
        double tlbi = double.Parse(txtKLBi.Text.Trim().Replace(",", "").Equals("") ? "0" : txtKLBi.Text.Trim().Replace(",", ""));

        txtKLHang.Text = ex.ConvertDecimal(tltong - tlbi);
        dlMacTX.Focus();
    }
    protected void txtDa12_TextChanged(object sender, EventArgs e)
    {
        TextBox thisTextBox = (TextBox)sender;
        double da12 = double.Parse(txtDa12.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDa12.Text.Trim().Replace(",", ""));
        double da24 = double.Parse(txtDa24.Text.Trim().Replace(",", "").Equals("") ? "0" : txtDa24.Text.Trim().Replace(",", ""));
        double catvang = double.Parse(txtCatVang.Text.Trim().Replace(",", "").Equals("") ? "0" : txtCatVang.Text.Trim().Replace(",", ""));
        double ximang = double.Parse(txtXiMang.Text.Trim().Replace(",", "").Equals("") ? "0" : txtXiMang.Text.Trim().Replace(",", ""));
        double nuoc = double.Parse(txtNuoc.Text.Trim().Replace(",", "").Equals("") ? "0" : txtNuoc.Text.Trim().Replace(",", ""));
        double phugia = double.Parse(txtPhuGia.Text.Trim().Replace(",", "").Equals("") ? "0" : txtPhuGia.Text.Trim().Replace(",", ""));

        txtTongKLTram.Text = ex.ConvertDecimal(da12 + da24 + catvang + ximang + nuoc + phugia);

        if (thisTextBox.ID.Equals("txtDa12"))
        {
            if (!txtDa12.Text.Trim().Replace(",", "").Equals(""))
                txtDa24.Text = "0";
            txtDa24.Focus();
        }
        else
            if (thisTextBox.ID.Equals("txtDa24"))
            {
                if (!txtDa24.Text.Trim().Replace(",", "").Equals(""))
                    txtDa12.Text = "0";
                txtCatVang.Focus();
            }
            else
                if (thisTextBox.ID.Equals("txtCatVang"))
                    txtXiMang.Focus();
                else
                    if (thisTextBox.ID.Equals("txtXiMang"))
                        txtNuoc.Focus();
                    else
                        if (thisTextBox.ID.Equals("txtNuoc"))
                            txtPhuGia.Focus();
    }
    protected void dlLoaiPhuGia_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtPhuGia.ReadOnly = dlLoaiPhuGia.SelectedIndex.Equals(0);
        if (dlLoaiPhuGia.SelectedIndex.Equals(0))
            txtPhuGia.Text = "";
    }
    protected void txtNgayBan_TextChanged(object sender, EventArgs e)
    {
        if (hdID.Value.Equals(""))
            LoadSoPhieu();
        LoadBienSoXe(dlBienSo);

        if (hdID.Value.Equals(""))
            btnSave.Visible = CheckKhoa2(txtNgayBan.Text.Trim(), frmName, Session["user"].ToString());
        else
            btncapnhat.Visible = CheckKhoa2(txtNgayBan.Text.Trim(), frmName, Session["user"].ToString());
    }
    private bool CheckKhoa(string ngaythang)
    {
        //load trang thai khoa
        if (!ngaythang.Trim().Equals(""))
        {

            p = new SqlParameter[3];
            p[0] = new SqlParameter("@Name", "BCXH");
            p[0].SqlDbType = SqlDbType.NVarChar;
            p[1] = new SqlParameter("@Thang", int.Parse(ngaythang.Trim().Split('/')[1]));
            p[1].SqlDbType = SqlDbType.Int;
            p[2] = new SqlParameter("@Nam", int.Parse(ngaythang.Trim().Split('/')[2]));
            p[2].SqlDbType = SqlDbType.Int;

            string index = ex.GetValueString("sp_xdakhoa_Check", p);

            if (index.Trim().Equals("") || index.Trim().Equals("1"))
            {
                lbTrangThai.Visible = false;
                return true;

            }
            else
            {
                lbTrangThai.Visible = true;
                return false;
            }
        }
        else
        {
            lbTrangThai.Visible = true;
            return false;

        }
    }
    protected void btnFirst_Click(object sender, EventArgs e)
    {
        CurrentPage = 1;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdaxuathang_List", p);
        GV.DataBind();
    }
    protected void btnPre_Click(object sender, EventArgs e)
    {
        if (CurrentPage > 1)
        {
            CurrentPage--;
            btnPre.Enabled = true;
            btnNext.Enabled = true;
            btnFirst.Enabled = true;

            p = new SqlParameter[2];
            p[0] = new SqlParameter("@PageNumber", CurrentPage);
            p[0].SqlDbType = SqlDbType.Int;
            p[1] = new SqlParameter("@RowspPage", PageNumber);
            p[1].SqlDbType = SqlDbType.Int;

            GV.DataSource = ex.GetData("sp_xdaxuathang_List", p);
            GV.DataBind();

        }
        else
        {
            btnFirst.Enabled = false;
            btnPre.Enabled = false;
            btnNext.Enabled = true;
        }
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        CurrentPage++;
        p = new SqlParameter[2];
        p[0] = new SqlParameter("@PageNumber", CurrentPage);
        p[0].SqlDbType = SqlDbType.Int;
        p[1] = new SqlParameter("@RowspPage", PageNumber);
        p[1].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdaxuathang_List", p);
        GV.DataBind();
    }
    protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string ID = e.CommandArgument.ToString();
        hdID.Value = ID;
        Guid gID = new Guid(ID);
        SqlParameter[] p;
        p = new SqlParameter[1];
        p[0] = new SqlParameter("@IDs", gID);
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;

        if (e.CommandName.Equals("Select"))
        {
            try
            {
                DataTable dt = ex.GetData("sp_xdaxuathang_ListByID", p);
                if (dt.Rows.Count > 0)
                {
                    //LoadCaLamViecAll(dlCaLamViec);
                    //LoadTramTronAll(dlTramTron);
                    //LoadBienSoXeAll(dlBienSo);
                    //LoadLaiXeAll(dlLaiXe);
                    //LoadPhuGiaAll(dlLoaiPhuGia);
                    //LoadCapPhoiAll(dlLoaiCapPhoi);
                    //LoadKhachHangAll(dlBenMua, "--Chọn khách hàng--");
                    //LoadCongTrinhAll(dlCongTrinh);
                    //LoadHangMucAll(dlHangMuc);
                    //LoadMacAll(dlMacKH, "--Chọn mác khách hàng--");
                    //LoadMacAll(dlMacTX, "--Chọn mác thực xuất--");


                    txtNgayBan.Text = dt.Rows[0]["NgayBan"].ToString();
                    txtNgayBan_TextChanged(sender, e);

                    txtSoPhieu.Text = dt.Rows[0]["SoPhieu"].ToString();
                    dlCaLamViec.SelectedValue = dt.Rows[0]["CaLamViec"].ToString();
                    txtGioBan.Text = dt.Rows[0]["GioXuat"].ToString();
                    dlTramTron.SelectedValue = dt.Rows[0]["TramTron"].ToString();
                    dlLoaiCaBom.SelectedValue = dt.Rows[0]["LoaiCaBom"].ToString();
                    dlBienSo.SelectedValue = dt.Rows[0]["BienSo"].ToString();
                    dlLaiXe.SelectedValue = dt.Rows[0]["LaiXe"].ToString();
                    dlLoaiSanPham.SelectedValue = dt.Rows[0]["LoaiSanPham"].ToString();
                    dlLoaiPhuGia.SelectedValue = dt.Rows[0]["LoaiPhuGia"].ToString();
                    dlLoaiCapPhoi.SelectedValue = dt.Rows[0]["LoaiCapPhoi"].ToString();
                    txtHoTroCuoc.Text = dt.Rows[0]["TienHoTroCuoc"].ToString();
                    dlBenMua.SelectedValue = dt.Rows[0]["BenMua"].ToString();
                    dlBenMua_SelectedIndexChanged(sender, e);

                    dlCongTrinh.SelectedValue = dt.Rows[0]["CongTrinh"].ToString();

                    dlCongTrinh_SelectedIndexChanged(sender, e);

                    dlHangMuc.SelectedValue = dt.Rows[0]["HangMuc"].ToString();

                    txtKLTong.Text = dt.Rows[0]["KLTong"].ToString();
                    txtKLBi.Text = dt.Rows[0]["KLBi"].ToString();
                    txtKLHang.Text = dt.Rows[0]["KLHang"].ToString();
                    dlMacKH.SelectedValue = dt.Rows[0]["MacKH"].ToString();
                    dlMacTX.SelectedValue = dt.Rows[0]["MacTX"].ToString();
                    txtDoSut.Text = dt.Rows[0]["DoSut"].ToString();
                    txtKLTX.Text = dt.Rows[0]["KLThucXuat"].ToString();
                    txtKLKhachHang.Text = dt.Rows[0]["KLKhachHang"].ToString();
                    txtKLNVKD.Text = dt.Rows[0]["KLNVKD"].ToString();
                    txtDa12.Text = dt.Rows[0]["Da12"].ToString();
                    txtDa24.Text = dt.Rows[0]["Da24"].ToString();
                    txtCatVang.Text = dt.Rows[0]["CatVang"].ToString();
                    txtNuoc.Text = dt.Rows[0]["Nuoc"].ToString();
                    txtXiMang.Text = dt.Rows[0]["XiMang"].ToString();
                    txtPhuGia.Text = dt.Rows[0]["PhuGia"].ToString();
                    txtTongKLTram.Text = dt.Rows[0]["KLTramTron"].ToString();
                    txtGhiChu.Text = dt.Rows[0]["GhiChu"].ToString();
                    txtGiaMuaBeTong.Text = dt.Rows[0]["GiaMua"].ToString();
                    //dlCaLamViec.Enabled = false;
                    //dlTramTron.Enabled = false;
                    //dlBienSo.Enabled = false;
                    dlLaiXe.Enabled = false;
                    //dlLoaiSanPham.Enabled = false;
                    //dlLoaiPhuGia.Enabled = false;
                    //dlLoaiCapPhoi.Enabled = false;
                    //dlBenMua.Enabled = false;
                    //dlCongTrinh.Enabled = false;
                    //dlHangMuc.Enabled = false;
                    //dlMacKH.Enabled = false;
                    //dlMacTX.Enabled = false;

                    
                    txtPhuGia.ReadOnly = dlLoaiPhuGia.SelectedIndex.Equals(0);
                    dlNhaCungCap.SelectedValue = dt.Rows[0]["NhaCungCap"].ToString();
                    ckMuaBeTong.Checked = dt.Rows[0]["MuaBeTong"].ToString().Equals("1") ? true : false;
                    dlNhaCungCap.Enabled = ckMuaBeTong.Checked;
                    txtGiaMuaBeTong.Enabled = ckMuaBeTong.Checked;
                }
                btncapnhat.Visible = true;
                btnSave.Visible = false;
                btncapnhat.Visible = CheckKhoa2(txtNgayBan.Text.Trim(), frmName, Session["user"].ToString());
                if (btncapnhat.Visible == false)
                {
                    txtNgayBan.Enabled = false;
                }
            }
            catch (Exception ax)
            {
                gstGetMess(ax.Message, "");
            }
        }
        else
            if (e.CommandName.Equals("Del"))
            {
                if (f_Xoa.Equals(1))
                {
                    try
                    {
                        ex.ExecuteNonQuerry("sp_xdaxuathang_Del", p);
                        btnCancel_Click(sender, e);
                        GetMess("Xóa thành công", "");
                    }
                    catch(Exception ax)
                    {
                        gstGetMess(ax.Message, "");
                    }
                }
                else
                    gstGetMess("Bạn không có quyền xóa trên trang này", "");

            }
    }
    protected void dlThang_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dlThang.SelectedIndex.Equals(0))
        {
            txtTuNgay.Visible = true;
            txtDenNgay.Visible = true;
            dlNam.Visible = false;
        }
        else
        {
            txtTuNgay.Visible = false;
            txtDenNgay.Visible = false;
            dlNam.Visible = true;
        }
        dlLoaiKH.SelectedIndex = 0;
        dlCongTrinhLoc.Items.Clear();
        dlCongTrinhLoc.Items.Insert(0, new ListItem("--Chọn công trình--", ""));
        dlCongTrinhLoc.SelectedIndex = 0;   
        
    }
    public void getDate()
    {
        if (dlThang.SelectedIndex.Equals(0))
        {
            hdTuNgay.Value = txtTuNgay.Text;
            hdDenNgay.Value = txtDenNgay.Text;
        }
        else
        {
            txtTuNgay.Text = "1/" + dlThang.SelectedValue + "/" + dlNam.SelectedValue;
            txtDenNgay.Text = GetLastDayOfMonth(int.Parse(dlThang.SelectedValue)).ToString("dd/MM/yyyy");
            hdTuNgay.Value = txtTuNgay.Text;
            hdDenNgay.Value = txtDenNgay.Text;
        }
    }
    public DateTime GetLastDayOfMonth(int iMonth)
    {
        DateTime dtResult = new DateTime(int.Parse(dlNam.SelectedValue), iMonth, 1);
        dtResult = dtResult.AddMonths(1);
        dtResult = dtResult.AddDays(-(dtResult.Day));
        return dtResult;
    }
    protected void dlLoaiKH_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dlLoaiKH.SelectedIndex > 0)
        {
            getDate();

            p = new SqlParameter[3];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;
            p[2] = new SqlParameter("@LoaiKH", ex.GetGuid(dlLoaiKH.SelectedValue));
            p[2].SqlDbType = SqlDbType.UniqueIdentifier;

            dlCongTrinhLoc.Items.Clear();
            dlCongTrinhLoc.DataSource = ex.GetData("sp_xdaxuathang_ListCongTrinhByLoaiKH_Search", p);
            dlCongTrinhLoc.DataBind();
            dlCongTrinhLoc.Items.Insert(0, new ListItem("--Chọn công trình--", ""));
            dlCongTrinhLoc.SelectedIndex = 0;
        }
        else
        {
            dlCongTrinhLoc.Items.Clear();
            dlCongTrinhLoc.Items.Insert(0, new ListItem("--Chọn công trình--", ""));
            dlCongTrinhLoc.SelectedIndex = 0;
        }
    }
    protected void btnXem_Click(object sender, EventArgs e)
    {
        int index=1;
        if(dlLoaiKH.SelectedIndex.Equals(0) && dlCongTrinhLoc.SelectedIndex.Equals(0)&& dlMacHD.SelectedIndex.Equals(0) && dlLoaiDa.SelectedIndex.Equals(0))
            index=1;
        else
            if (dlLoaiKH.SelectedIndex > 0 && dlCongTrinhLoc.SelectedIndex.Equals(0) && dlMacHD.SelectedIndex.Equals(0) && dlLoaiDa.SelectedIndex.Equals(0))
            index=2;
            else if (dlLoaiKH.SelectedIndex > 0 && dlCongTrinhLoc.SelectedIndex > 0 && dlMacHD.SelectedIndex.Equals(0) && dlLoaiDa.SelectedIndex.Equals(0))
            index=3;
            else if (dlLoaiKH.SelectedIndex > 0 && dlCongTrinhLoc.SelectedIndex > 0 && dlMacHD.SelectedIndex>0 && dlLoaiDa.SelectedIndex.Equals(0))
                index = 4;
            else if (dlLoaiKH.SelectedIndex > 0 && dlCongTrinhLoc.SelectedIndex > 0 && dlMacHD.SelectedIndex>0 && dlLoaiDa.SelectedIndex>0)
                index = 5;

        getDate();
        p = new SqlParameter[7];
        p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
        p[0].SqlDbType = SqlDbType.SmallDateTime;
        p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
        p[1].SqlDbType = SqlDbType.SmallDateTime;
        p[2] = new SqlParameter("@LoaiKH", ex.GetGuid(dlLoaiKH.SelectedValue));
        p[2].SqlDbType = SqlDbType.UniqueIdentifier;
        p[3] = new SqlParameter("@CongTrinh", ex.GetGuid(dlCongTrinhLoc.SelectedValue));
        p[3].SqlDbType = SqlDbType.UniqueIdentifier;
        p[4] = new SqlParameter("@Index",index);
        p[4].SqlDbType = SqlDbType.Int;
        p[5] = new SqlParameter("@MacHD", ex.GetGuid(dlMacHD.SelectedValue));
        p[5].SqlDbType = SqlDbType.UniqueIdentifier;
        p[6] = new SqlParameter("@LoaiDa", int.Parse(dlLoaiDa.SelectedValue));
        p[6].SqlDbType = SqlDbType.Int;

        GV.DataSource = ex.GetData("sp_xdaxuathang_Search", p);
        GV.DataBind();

        
        //get footer
        p = new SqlParameter[7];
        p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
        p[0].SqlDbType = SqlDbType.SmallDateTime;
        p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
        p[1].SqlDbType = SqlDbType.SmallDateTime;
        p[2] = new SqlParameter("@LoaiKH", ex.GetGuid(dlLoaiKH.SelectedValue));
        p[2].SqlDbType = SqlDbType.UniqueIdentifier;
        p[3] = new SqlParameter("@CongTrinh", ex.GetGuid(dlCongTrinhLoc.SelectedValue));
        p[3].SqlDbType = SqlDbType.UniqueIdentifier;
        p[4] = new SqlParameter("@Index", index);
        p[4].SqlDbType = SqlDbType.Int;
        p[5] = new SqlParameter("@MacHD", ex.GetGuid(dlMacHD.SelectedValue));
        p[5].SqlDbType = SqlDbType.UniqueIdentifier;
        p[6] = new SqlParameter("@LoaiDa", int.Parse(dlLoaiDa.SelectedValue));
        p[6].SqlDbType = SqlDbType.Int;

        dt = ex.GetData("sp_xdaxuathang_Search_GetFooter", p);
        if (dt.Rows.Count > 0)
        {
            double kltx = double.Parse(dt.Rows[0]["KLThucXuat"].ToString());
            double klkh = double.Parse(dt.Rows[0]["KLKhachHang"].ToString());
            

            GV.FooterRow.Cells[7].Text = "Tổng cộng";
            GV.FooterRow.Cells[7].HorizontalAlign = HorizontalAlign.Center;
            GV.FooterRow.Cells[11].Text = ConvertDecimal(kltx);
            GV.FooterRow.Cells[12].Text = ConvertDecimal(klkh);
            
        }

        btnNext.Visible = false;
        btnPre.Visible = false;
        btnFirst.Visible = false;
    }
    string ConvertDecimal(double s)
    {
        string q = "";
        if (s.ToString().Contains("."))
        {
            double x = double.Parse(s.ToString().Substring(0, s.ToString().IndexOf('.')));
            q = (x != 0 && x > 999) ? string.Format("{0:0,0}", x) : x.ToString();
            q += s.ToString().Substring(s.ToString().IndexOf('.'));
        }
        else
            q = (s != 0 && s > 999) ? string.Format("{0:0,0}", s) : s.ToString();
        return q;
    }
    protected void ckMuaBeTong_CheckedChanged(object sender, EventArgs e)
    {
        dlNhaCungCap.Enabled = ckMuaBeTong.Checked;
        txtGiaMuaBeTong.Enabled = ckMuaBeTong.Checked;
        dlNhaCungCap.SelectedIndex = 0;
        txtGiaMuaBeTong.Text = "";
    }
    void LoadNhaCungCap(DropDownList dl, string title)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData("sp_xdanhacungcap_LoadDrop");
        dl.DataBind();
        dl.Items.Insert(0, new ListItem(title, ""));
        dl.SelectedIndex = 0;
    }
    protected void dlCongTrinhLoc_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dlCongTrinhLoc.SelectedIndex > 0)
        {
            p = new SqlParameter[3];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;
            p[2] = new SqlParameter("@CongTrinh", ex.GetGuid(dlCongTrinhLoc.SelectedValue));
            p[2].SqlDbType = SqlDbType.UniqueIdentifier;

            dlMacHD.Items.Clear();
            dlMacHD.DataSource = ex.GetData("sp_BCXH_LoadMacHDByCongTrinh", p);
            dlMacHD.DataBind();
            dlMacHD.Items.Insert(0, new ListItem("--Chọn mác HĐ--", ""));
            dlMacHD.SelectedIndex = 0;
        }
        else
            LoadMacHD(dlMacHD);
        LoadLoaiDa(dlLoaiDa);
    }
    protected void dlMacHD_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dlMacHD.SelectedIndex > 0)
        {
            p = new SqlParameter[4];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;
            p[2] = new SqlParameter("@CongTrinh", ex.GetGuid(dlCongTrinhLoc.SelectedValue));
            p[2].SqlDbType = SqlDbType.UniqueIdentifier;
            p[3] = new SqlParameter("@Mac", ex.GetGuid(dlMacHD.SelectedValue));
            p[3].SqlDbType = SqlDbType.UniqueIdentifier;

            dlLoaiDa.Items.Clear();
            dlLoaiDa.DataSource = ex.GetData("sp_BCXH_LoadLoaiDaByMacCongTrinh", p);
            dlLoaiDa.DataBind();
            dlLoaiDa.Items.Insert(0, new ListItem("--Chọn loại đá--", "0"));
            dlLoaiDa.SelectedIndex = 0;
        }
        else
            LoadLoaiDa(dlLoaiDa);
    }
    protected void btnTimKiem2_Click(object sender, EventArgs e)
    {
        try
        {
            getDate();
            p = new SqlParameter[3];
            p[0] = new SqlParameter("@TuNgay", ex.GetNgayThang(hdTuNgay.Value));
            p[0].SqlDbType = SqlDbType.SmallDateTime;
            p[1] = new SqlParameter("@DenNgay", ex.GetNgayThang(hdDenNgay.Value));
            p[1].SqlDbType = SqlDbType.SmallDateTime;
            p[2] = new SqlParameter("@Key", txtKey.Text.Trim());
            p[2].SqlDbType = SqlDbType.NVarChar;

            GV.DataSource = ex.GetData("sp_xdaxuathang_SearchKey", p);
            GV.DataBind();
        }
        catch (Exception ax)
        {
            gstGetMess(ax.Message, "");
        }

    }
}